<?php

namespace app\api\controller;

use app\common\library\storage\Driver as StorageDriver;
use app\shop\model\settings\Setting as SettingModel;

/**
 * 上传
 */
class Uploads
{
    /**
     * 上传图片
     */
    public function uploadImg()
    {
        set_time_limit(0);
        $file_type = 'image';
        // 实例化存储驱动
        $config = SettingModel::getItem('storage');

        $StorageDriver = new StorageDriver($config);
        // 图片信息
        $fileInfo = request()->file('file');
        // 校验
        if(!$StorageDriver->validate('file', $fileInfo, $file_type)){
            return json(['code' => 0, 'msg' => $StorageDriver->getError()]);
        }
        // 设置上传文件的信息
        $StorageDriver->setUploadFile('file');

        // 上传图片
        $saveName = $StorageDriver->upload();
        if ($saveName == '') {
            return json(['code' => 0, 'msg' => '文件上传失败' . $StorageDriver->getError()]);
        }

        $saveName = str_replace('\\','/',$saveName);

        // 图片上传路径
        $fileName = $StorageDriver->getFileName();
        // 图片上传成功
        return json(['code' => 1, 'msg' => '文件上传成功', 'data' => 'https://plusone-stock.com/public/uploads/' . $saveName]);
    }
}
